package DAOJPA;

import film.Film;
import film.Participer;

import java.util.List;

import javax.persistence.Query;

import DAO.DAO;
import DAO.DAOParticiper;

public class DAOParticiperJPA extends DAO implements DAOParticiper{

	static private DAOParticiperJPA instance = null;
	
	private DAOParticiperJPA() {
	}
		static public DAOParticiper getInstance() {
		 	if (instance == null)
				instance = new DAOParticiperJPA();
			 	
		 	return instance;
	}
	
	@Override
	public Participer getParticipation(int code, int code2) {
		// TODO Auto-generated method stub
		return DAO.getManager().createQuery("SELECT a FROM Participer a WHERE a.id.code_per =:code AND a.id.code_flm =:code2 ", Participer.class).setParameter("code", code).setParameter("code2", code2).getSingleResult();
		
	}
	
	@Override
	public List<Participer> getParticipationInvalide() {
		// TODO Auto-generated method stub
		return DAO.getManager().createQuery("SELECT a FROM Participer a WHERE a.valide =:code ", Participer.class).setParameter("code", 1).getResultList();
		
	}
	
	@Override
	public List<Participer> getFilms(int code) {
		// TODO Auto-generated method stub
		return DAO.getManager().createQuery("SELECT a FROM Participer a WHERE a.id.code_per =:code AND a.valide = :vrai", Participer.class).setParameter("code", code).setParameter("vrai", 0).getResultList();
		
	}
	
	@Override
	public List<Participer> getPersonnes(int code) {
		// TODO Auto-generated method stub
		return DAO.getManager().createQuery("SELECT a FROM Participer a WHERE a.id.code_film =:code AND a.valide = :vrai", Participer.class).setParameter("code", code).setParameter("vrai", 0).getResultList();
		
	}
	
	
	@Override
	public List<Participer> loadAll() {
		// TODO Auto-generated method stub
		return DAO.getManager().createQuery("SELECT a FROM Participer a", Participer.class).getResultList();

	}

	@Override
	public int getNombreParticiper() {
		// TODO Auto-generated method stub
		return this.loadAll().size();
	}

	@Override
	public void save(Participer a) {
		// TODO Auto-generated method stub
		DAO.getManager().persist(a);
		DAO.commit();
		
	}

	@Override
	public void delete(Participer a) {
		// TODO Auto-generated method stub
		getManager().remove(a);
		DAO.commit();
		
	}
	
	@Override
	public void deleteQuery(Participer a)
	{
		Query query = DAO.getManager().createQuery("DELETE FROM Participer a WHERE a.id.code_film = :p1 AND a.id.code_per = :p2 AND a.valide = :p3 AND a.date_creation = :p4" , Participer.class);
		query.setParameter("p1", a.getId().getCode_film()).setParameter("p2", a.getId().getCode_per()).setParameter("p3", 1).setParameter("p4", a.getDate_creation()).executeUpdate();
		DAO.commit();
	}

}
